<?php

/**
 * 文章控制器
 *
 * @author MrHan<hcj112@163.com>
 */

class ArticleController extends CommonController
{
    public function index()
    {
        $this->display();
    }


    public function add()
    {

        $this->display();
    }


    /**
     * 添加文章
     */
    public function insert()
    {
        $Article = D('Article');
        if ($vo = $Article->create()) {
            dump($vo);
            exit;
            $list = $Article->add($vo);
            if ($list) {
                //执行添加标签
                if (method_exists($this, 'saveTag')) {
                    $this->saveTag($vo, $list, 'Article');
                }
                $this->success('添加文章成功');
            } else {
                $this->error('添加文章失败');
            }
        } else {
            $this->error($Article->getError());
        }
    }



    /**
     * 保存标签
     *
     * @param $vo  保存的数据
     * @param $list 文章ID
     * @param $module 模型
     *
     */
    public function saveTag($vo, $st, $module)
    {
        if (!empty($vo) && !empty($list)) {
            $Tag = M('Tag');
            $Tagged = M('Tagged');
            $tags = explode(',', $vo['tags']);

            foreach ($tags as $val) {
                $val = trim($val);
                if (!empty($val)) {
                    $map['module'] = 'Article';
                    $map['name'] = $val;
                    $tagg = $Tag->where($map)->find();
                    //当tag表中有了这个标签,count字段加1;
                    if ($tagg) {
                        $tagId = $tagg['id'];
                        $Tag->where('id=' . $tagId)->setInc('count');
                    } else {
                        $t = array();
                        import('ORG.Util.Pinyin');
                        $pinYin = new Pinyin();
                        $t['pinyin'] = $pinYin->pin($val);
                        $t['module'] = $module;
                        $t['name'] = $val;
                        $t['count'] = 1;
                        $result = $Tag->add($t);
                        $tagId = $result;
                    }
                }
                $t = array();
                $t['module'] = $module;
                $t['article_id'] = $list;
                $t['tag_id'] = $tagId;
                $t['create_time'] = time();
                $Tagged->add($t);
            }
        }
    }


    /**
     * 修改标签
     *
     * @param $vo
     * @param $list
     * @param $module
     */
    public function updateTag($vo, $list, $module)
    {
        if (!empty($vo) && !empty($list)) {
            $Tag = M('Tag');
            $Tagged = M('Tagged');

            // 根据文章ID查询出原来tagged的tag_id
            $oldTag = $Tagged->where('record_id=' . $list)->select();
            // 如果该标签count字段大于1,则把count-1,否则直接删除原来的标签
            foreach ($oldTag as $value) {
                $result = $Tag->where('id=' . $value['tag_id'])->find();
                if ($result['count'] > 1) {
                    $Tag->where('id=' . $result['id'])->setDec('count');
                } else {
                    $Tag->where('id=' . $result['id'])->delete();
                }

            }

            // 把原来tagged表中关于这篇文章的信息删除,然后重新插入修改后的标签信息
            $Tagged->where('record_id=' . $list)->delete();

            $tags = explode(',', $vo['tags']);
            foreach ($tags as $val) {
                $val = trim($val);
                if (!empty($val)) {
                    $map['module'] = 'Article';
                    $map['name'] = $val;
                    $tagg = $Tag->where($map)->find();
                    //当tag表中有了这个标签,count字段加1;
                    if ($tagg) {
                        $tagId = $tagg['id'];
                        $Tag->where('id=' . $tagId)->setInc('count');
                    } else {
                        $t = array();
                        import('ORG.Util.Pinyin');
                        $pinYin = new Pinyin();
                        $t['pinyin'] = $pinYin->pin($val);
                        $t['module'] = $module;
                        $t['name'] = $val;
                        $t['count'] = 1;
                        $result = $Tag->add($t);
                        $tagId = $result;
                    }
                }
                $t = array();
                $t['module'] = $module;
                $t['article_id'] = $list;
                $t['tag_id'] = $tagId;
                $t['create_time'] = time();
                $Tagged->add($t);
            }
        }
    }


    public function edit()
    {
        $list = D('Article')->getArticleByID();
        $this->assign('list', $list);
        $this->display();
    }

    // 修改文章
    public function update()
    {

        $Article = D('Article');
        $vo = $Article->create();
        if (!$vo) {
            $this->error($Article->getError());
        }

        // 文章ID
        $aid = is_array($vo) ? $vo[$Article->getPk()] : $vo->{$Article->getPk()};
        $result = $Article->save();
        if ($result) {
            if (method_exists($this, 'updateTag')) {
                $this->updateTag($vo, $aid, 'Article');
            }
            $this->success('修改成功');
        } else {
            $this->error($Article->getError());
        }
    }


}